#pragma once 
#include<iostream>
#include <atomic>
#include "marketData.hpp"

//noop: original status
//openning:opening position
//closing:closing position
//zero:terminate position,control postion to zero
//suspend:pause trading until recover
//unknown:unknown status
//error:error status
enum class State {
	noop, openning, closing, zero, error,unknown
};

enum class tradingStatus {
	normal,suspend,terminate
};


class position {
public:
	State state = State::noop;
	int initialPosition = 0;
	int modifiedPosition = 0;
	int openOrderId = -1;
	int lastOrderId = -1;
	int bought = { 0 };
	int sold = { 0 };
	int buying = { 0 };
	int selling = { 0 };
	double amount = { 0 };
	double totalAmount = { 0 };
	double fee = { 0 };
	double profit = { 0 };
	double lastPrice = { 0 };

	std::string getStatus() {
		if (state == State::noop) {
			return "noop";
		}
		else if (state == State::openning) {
			return "openning";
		}
		else if (state == State::closing) {
			return "closing";
		}
		else if (state == State::zero) {
			return "zero";
		}
		else if (state == State::error) {
			return "error";
		}
		else if (state == State::unknown) {
			return "unknown";
		}
		return "unknown";
	}
};

class status :tickPoint{
private:


public:
	int code;
	int tick;
	std::vector<position> mypos;
	int totalinitialPosition=0;
	int totalmodifiedPosition=0;
	int totalbought=0;
	int totalsold=0;
	int totalbuying=0;
	int totalselling=0;

	void initialize(std::vector<config> myconfiglist) {
		for (int i = 0; i < myconfiglist.size(); i++) {
			auto& myconfig = myconfiglist[i];
			position pos;
			pos.initialPosition = myconfig.initialPosition;
		}
	}
	

	bool update(marketData lastMd) {
		auto& lastIndex = this->lastIndex;
		auto& lastTick = this->lastTick;
		auto& index = this->index;
		auto& tick = this->tick;
		int tickNow = lastMd.tick;
		int indexNow = this->updatetimeToIndex(tickNow);
		this->totalinitialPosition = 0;
		this->totalmodifiedPosition = 0;
		this->totalbought = 0;
		this->totalsold = 0;
		this->totalbuying = 0;
		this->totalselling = 0;
		if (indexNow >= index) {
			// update posNow
			for (int i = 0; i < mypos.size(); i++) {
				//get position information from hzk
				//sum
				this->totalinitialPosition += mypos[i].initialPosition;
				this->totalmodifiedPosition += mypos[i].modifiedPosition;
				this->totalbought += mypos[i].bought;
				this->totalsold += mypos[i].sold;
				this->totalbuying += mypos[i].buying;
				this->totalselling += mypos[i].selling;
			}
			lastIndex = index;
			lastTick = tick;
			index = indexNow;
			tick = tickNow;

		}
	}
};
